package com.rainliang.project.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
	/**
	 * 获取cookie
	 * @param request
	 * @param key
	 * @return
	 */
	public static String getCookieBean(HttpServletRequest request , String key){
		 String cookieId = null;
		 Cookie cookies[] = request.getCookies();
		 for(int i=0;cookies!=null&&i<cookies.length;i++){
			 if(cookies[i].getName().equals(key)){
				 cookieId = cookies[i].getValue();
				 break;
			 }
		 }
		 return cookieId;
	 }
	
	/**
	 * 设置cookie
	 * @param response
	 * @param key
	 * @param value
	 */
	public static void setCookieBean(HttpServletResponse response , String key , String value){
		Cookie val_login_email = new Cookie(key , value);
		val_login_email.setPath("/");
		val_login_email.setMaxAge(1000*60*10);
		response.addCookie(val_login_email);
	}
	/**
	 * 设置cookie
	 * @param response
	 * @param key
	 * @param value
	 */
	public static void setCookieBean_noMaxAge(HttpServletResponse response , String key , String value){
		Cookie val_login_email = new Cookie(key , value);
		val_login_email.setPath("/");
		response.addCookie(val_login_email);
	}
}
